﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using log4net;
using System.IO;

namespace Repo7.BusinessLogic.Local
{
    /// <summary>
    /// Local Repository Watcher
    /// </summary>
    public class RepoWatcher
    {
        #region Private Members

        /// <summary>
        /// Logger
        /// </summary>
        protected static readonly ILog logger = LogManager.GetLogger(typeof(RepoWatcher));  

        /// <summary>
        /// Repository path
        /// </summary>
        private string repositoryFolderPath;

        /// <summary>
        /// File System Watcher
        /// </summary>
        private FileSystemWatcher watcher;
        
        #endregion

        #region Constructors

        public RepoWatcher(string repositoryPath)
        { 
            repositoryFolderPath = repositoryPath;
            watcher = new FileSystemWatcher(repositoryPath);
        }

        #endregion

        #region Public Methods

        public void StartObservingRepository()
        {
            watcher.EnableRaisingEvents = true;
            logger.Info("Started observing folder...");
        }

        #endregion

        #region Private Methods

        /// <summary>
        /// Setup watcher methods
        /// </summary>
        public void SetupWatcherEventHandlers(FileSystemEventHandler onCreated, FileSystemEventHandler onChanged, 
            FileSystemEventHandler onDeleted, RenamedEventHandler onRenamed)
        {
            if (onCreated != null)
            {
                watcher.Created += onCreated;
            }

            if (onChanged != null)
            {
                watcher.Changed += onChanged;
            }

            if (onDeleted != null)
            {
                watcher.Deleted += onDeleted;
            }

            if (onRenamed != null)
            {
                watcher.Renamed += onRenamed;
            }
        }

        #region Old methods
        
        ///// <summary>
        ///// On Created Event Handler
        ///// </summary>
        ///// <param name="sender"></param>
        ///// <param name="eventArgs"></param>
        //private void OnCreated(object sender, FileSystemEventArgs eventArgs)
        //{
        //    //Ignore changes to repo.info
        //    if (eventArgs.Name.Equals(RepoInfoManager.repositoryInfoFileName))
        //    {
        //        return;
        //    }

        //    FileInfo newFile = new FileInfo(eventArgs.FullPath);
        //    var repoFileInfo = FileUtils.CreateRepositoryFile(newFile);
        //    repositoryInfoManager.AddFile(repoFileInfo);
        //    logger.Info(string.Format("Added file '{0}' to the repository.", eventArgs.Name));
        //}

        ///// <summary>
        ///// On Change Event Handler
        ///// </summary>
        ///// <param name="sender"></param>
        ///// <param name="eventArgs"></param>
        //private void OnChanged(object sender, FileSystemEventArgs eventArgs)
        //{
        //    //Ignore changes to repo.info
        //    if (eventArgs.Name.Equals(RepoInfoManager.repositoryInfoFileName))
        //    {
        //        return;
        //    }

        //    FileInfo changedFile = new FileInfo(eventArgs.FullPath);
        //    var repoChange = FileUtils.CreateRepositoryFile(changedFile);
        //    repositoryInfoManager.UpdateFile(repoChange);
        //    logger.Info(string.Format("modified file '{0}' from repository.", eventArgs.Name));
        //}

        ///// <summary>
        ///// On Deleted Event Handler
        ///// </summary>
        ///// <param name="sender"></param>
        ///// <param name="eventArgs"></param>
        //private void OnDeleted(object sender, FileSystemEventArgs eventArgs)
        //{
        //    //Ignore changes to repo.info
        //    if (eventArgs.Name.Equals(RepoInfoManager.repositoryInfoFileName))
        //    {
        //        return;
        //    }

        //    repositoryInfoManager.DeleteFile(eventArgs.Name);
        //    logger.Info(string.Format("Deleted file '{0}'.", eventArgs.Name));
        //}

        ///// <summary>
        ///// On Renamed Event Handler
        ///// </summary>
        ///// <param name="sender"></param>
        ///// <param name="eventArgs"></param>
        //private void OnRenamed(object sender, RenamedEventArgs eventArgs)
        //{
        //    //Ignore changes to repo.info
        //    if (eventArgs.Name.Equals(RepoInfoManager.repositoryInfoFileName))
        //    {
        //        return;
        //    }

        //    FileInfo changedFile = new FileInfo(eventArgs.FullPath);
        //    var repoChange = FileUtils.CreateRepositoryFile(changedFile);
        //    repositoryInfoManager.UpdateFile(repoChange);
        //    logger.Info(string.Format("Renamed file '{0}'.", eventArgs.Name));
        //}

        #endregion

        #endregion
    }
}
